package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import values.Comment;
import values.RelatedComment;

import core.Core;
import exception.ServerDownException;

public class Comments extends HttpServlet
{
	// Constants
	
	/**
	 * Auto generated serial.
	 */
	private static final long serialVersionUID = 2401893193021502682L;

	/**
	 * Servlet function, add comment
	 */
	private static final String ADD_COMMENT="addComment";
	
	/**
	 * Servlet function, add a related comment
	 */
	private static final String RELATED = "related";
	
	/**
	 * Servlet function, add helpful vote.
	 */
	private static final String HELPFUL = "helpful";
	
	/**
	 * Servlet function, report innapropiate.
	 */
	private static final String REPORT = "report";
	
	/**
	 * Parameter name, comment
	 */
	private static final String COMMENT="comment";
	
	//Constructor
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession s = request.getSession();
		String login = (String)s.getAttribute(Gente.LOGIN);
		
		request.setCharacterEncoding("UTF-8");
		
		String function = request.getParameter(Gente.FUNCTION);
		
		if(function!=null && login != null && !login.equals(""))
		{
			if(function.equals(ADD_COMMENT))
			{
				String header = request.getParameter(ADD_COMMENT);
				String comment = request.getParameter(COMMENT);
				
				if(header != null && comment != null && !header.equals("") && !comment.equals(""))
				{
					try
					{
						Core.insertComment(login, comment, header);
					}
					catch (ServerDownException e) 
					{
						System.out.println(e.getLocalizedMessage());
						e.printStackTrace();
						
						response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
					}
				}
			}
			else if (function.equals(RELATED))
			{
				String commentId = request.getParameter(COMMENT);
				String related = request.getParameter(RELATED);
				
				if(commentId != null && !commentId.equals("") && related != null && !related.equals(""))
				{
					try
					{
						Core.insertRelatedComment(Integer.parseInt(commentId), login, related);
						
						Comment c = Core.getComment(Integer.parseInt(commentId));
						RelatedComment rel = c.getComments().get(c.getComments().size() - 1);
						
						response.setContentType("text/html");
						PrintWriter out = response.getWriter();
						
						Bancos.printRelated(out, rel, true, false, true);
						
						out.flush();
						out.close();
					}
					catch(Exception e)
					{
						System.out.println(e.getLocalizedMessage());
						e.printStackTrace();
						
						response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
					}
				}
			}
			else if (function.equals(HELPFUL))
			{
				try
				{
					String commentId = request.getParameter(COMMENT);
					
					Core.helpful(Integer.parseInt(commentId), login);
					
					Comment c = Core.getComment(Integer.parseInt(commentId));
					
					response.setContentType("text/html");
					PrintWriter pr = response.getWriter();
					pr.println(c.getHelpful());
					pr.flush();
					pr.close();
				}
				catch(Exception e)
				{
					System.out.println(e.getLocalizedMessage());
					e.printStackTrace();
					
					response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
				}
			}
			else if (function.equals(REPORT))
			{
				try
				{
					String commentId = request.getParameter(COMMENT);
					
					Core.report(Integer.parseInt(commentId), login);
					
					response.setContentType("text/html");
					PrintWriter pr = response.getWriter();
					pr.println(login);
					pr.flush();
					pr.close();
				}
				catch(Exception e)
				{
					System.out.println(e.getLocalizedMessage());
					e.printStackTrace();
					
					response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
				}
			}
		}	
	}
}
